//
//  LSYDiscoverSearchView.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/7.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit
import SnapKit

class LSYDiscoverSearchView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置视图大小
        self.frame = CGRect(x: 0, y: 20, width: UIScreen.mainScreen().bounds.width, height: 44)
        
//        self.backgroundColor = UIColor.redColor()
        
        // 添加子控件
        addSubview(cancelButton)
        addSubview(searchField)
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    var rightConstrain : Constraint?
    
    override func layoutSubviews() {
        
        // 设置 textField 初始约束
        searchField.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(superview!).offset(10)
            make.trailing.equalTo(superview!).offset(-10)
            make.top.equalTo(10)
            make.height.equalTo(34)

        }
        
        // 设置 cancelButton 约束
        cancelButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(superview!).offset(-10)
            make.centerY.equalTo(searchField)
        }
    }
    
    
    // MARK: - 懒加载 searchField
    lazy var searchField:LSYTextField = {
        
        let searchTF = LSYTextField()
        
        searchTF.leftView = UIImageView(image: UIImage(named:"searchbar_textfield_search_icon"))
        searchTF.leftViewMode = .Always
        searchTF.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0)
        
        searchTF.layer.cornerRadius = 10
        searchTF.layer.borderWidth = 2
        searchTF.layer.borderColor = UIColor.orangeColor().CGColor
        
        /** 添加编辑事件 */
        searchTF.addTarget(self, action: "editingBegin", forControlEvents: .EditingDidBegin)
        //        searchTF.addTarget(self, action: "editingEnd", forControlEvents: .EditingDidEnd)
        
        return searchTF
    }()
    
    /** 开始编辑调用方法 */
    @objc private func editingBegin(){
        
        print("编辑开始")
        
//        self.rightConstrain?.updateOffset(-cancelButton.frame.width-10)
        
        //  MARK: - 报错:有多余约束,未解决
//        self.searchField.snp_removeConstraints()
//        self.searchField.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(superview!).offset(10)
//            make.trailing.equalTo(superview!).offset(-cancelButton.frame.width-10)
//            make.top.equalTo(10)
//            make.height.equalTo(34)
        
        
        self.searchField.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(superview!).offset(-cancelButton.frame.width - 10)
        }

        UIView.animateWithDuration(0.25) { () -> Void in
            // 提前刷新页面
            // layoutIfNeeded是根据 自动布局 来修改frame
            self.layoutIfNeeded()
        }
    }
    
    /** 结束编辑调用方法 */
    @objc private func editingEnd(){
        
        print("编辑完成")
        
        self.searchField.snp_removeConstraints()
        self.searchField.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(superview!).offset(10)
            make.trailing.equalTo(superview!).offset(-10)
            make.top.equalTo(10)
            make.height.equalTo(34)
        }
        
        UIView.animateWithDuration(0.25) { () -> Void in
            // 提前刷新页面
            // layoutIfNeeded是根据 自动布局 来修改frame
            self.layoutIfNeeded()
        }
        
        // 取消第一响应者
        self.searchField.resignFirstResponder()
    }
    
    
    // MARK: - 懒加载 cancelButton
    lazy var cancelButton: UIButton = {
       
        let cancelButton = UIButton(type: .Custom)
        
        cancelButton.setTitle("取消", forState: .Normal)
        cancelButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        // 取消按钮点击事件
        cancelButton.addTarget(self, action: "cancelEdit", forControlEvents: .TouchUpInside)
        
        return cancelButton
    }()
    
    
    /** 取消按钮点击事件 */
    @objc private func cancelEdit(){
        
        // 结束编辑方法
        editingEnd()
    }
    
    
    

}
